package com.ssy.lingxi.product.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.*;
import com.ssy.lingxi.product.api.model.response.GoodsAndProcessResponse;
import com.ssy.lingxi.product.api.model.response.GoodsResponse;
import com.ssy.lingxi.product.api.model.response.GoodsSourceResponse;
import com.ssy.lingxi.product.api.model.response.GoodsSubResponse;
import com.ssy.lingxi.product.common.enums.MaterialsAddInternalStatusEnum;
import com.ssy.lingxi.product.entity.Goods;
import com.ssy.lingxi.product.entity.GoodsSupplyList;
import com.ssy.lingxi.product.model.request.MaterialExamineRequest;
import com.ssy.lingxi.product.model.resp.MaterInnerLogResponse;
import com.ssy.lingxi.product.model.resp.MaterialInnerStatusResponse;
import com.ssy.lingxi.product.service.IGoodsService;
import com.ssy.lingxi.product.service.customer.ICustomerAttributeValueService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author jw.chen
 * @version 2.0.0
 * @menu: 货品管理
 * 货品管理
 * @date 2020/6/30
 */
@RestController
@RequestMapping("/product/goods")
public class GoodsController extends BaseController {

    @Resource
    private IGoodsService goodsService;
    @Resource
    private ICustomerAttributeValueService customerAttributeValueService;

    /**
     * 物料管理-物料查询-获取物料工作流程全部状态
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/getInnerStatus", method = RequestMethod.GET)
    public Wrapper<List<MaterialInnerStatusResponse>> getInnerStatus() {
        return Wrapper.success(goodsService.getInnerStatus());
    }

    /**
     * 物料管理-物料查询-详情-内部单据流转记录
     *
     * @param goodsId 物料id
     * @return 操作结果
     */
    @RequestMapping(value = "/getMaterInnerLogList", method = RequestMethod.GET)
    public Wrapper<List<MaterInnerLogResponse>> getMaterInnerLogList(Long goodsId) {
        return Wrapper.success(goodsService.getMaterInnerLogList(goodsId));
    }

    /**
     * 物料管理-物料查询-列表
     *
     * @param pageVO            分页实体
     * @param goodsQueryRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getMaterialList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsResponse>> getMaterialList(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getMaterialList(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        //设置生产厂家和产地
        goodsService.setGoodsResponseData(resultList, result.getContent(), sysUser);
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-物料查询-冻结/启用
     *
     * @param goodsFreezeEnableRequest 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "/freezeOrEnableGoods", method = RequestMethod.POST)
    public Wrapper<String> freezeOrEnableGoods(@RequestBody @Validated GoodsFreezeEnableRequest goodsFreezeEnableRequest) {
        //冻结
        if (MaterialsAddInternalStatusEnum.FROZEN.getCode().equals(goodsFreezeEnableRequest.getStatus())) {
            return Wrapper.success(goodsService.freezeGoods(this.getSysUser(), goodsFreezeEnableRequest));
        } else {
            //启用
            return Wrapper.success(goodsService.enableGoods(this.getSysUser(), goodsFreezeEnableRequest));
        }
    }

    /**
     * 物料管理-物料查询-冻结/启用(批量)
     *
     * @param goodsFreezeEnableBatchRequest 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "/freezeOrEnableGoodsBatch", method = RequestMethod.POST)
    public Wrapper<String> freezeOrEnableGoodsBatch(@RequestBody @Validated GoodsFreezeEnableBatchRequest goodsFreezeEnableBatchRequest) {
        //批量冻结
        if (MaterialsAddInternalStatusEnum.FROZEN.getCode().equals(goodsFreezeEnableBatchRequest.getStatus())) {
            return Wrapper.success(goodsService.freezeGoodsBatch(this.getSysUser(), goodsFreezeEnableBatchRequest));
        } else {
            //批量启用
            return Wrapper.success(goodsService.enableGoodsBatch(this.getSysUser(), goodsFreezeEnableBatchRequest));
        }
    }

    /**
     * 物料管理-物料查询-物料清单-列表
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "/getGoodsSourceList", method = RequestMethod.GET)
    public Wrapper<List<GoodsSupplyList>> getGoodsSourceList(SimpleIdRequest request) {
        return Wrapper.success(goodsService.getGoodsSourceList(request));
    }

    /**
     * 物料管理-物料查询-物料清单-提交保存
     *
     * @param list 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "/submitGoodsSourceList", method = RequestMethod.POST)
    public Wrapper<String> submitGoodsSourceList(@RequestBody @Validated GoodsSupplyRequest list) {
        return Wrapper.success(goodsService.submitGoodsSourceList(list));
    }

    /**
     * 物料管理-查询货品列表
     *
     * @param pageVO            分页实体
     * @param goodsQueryRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsResponse>> getGoodsList(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getGoodsList(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        //设置生产厂家和产地
        goodsService.setGoodsResponseData(resultList, result.getContent(), sysUser);
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待新增物料列表
     *
     * @param pageVO            分页实体
     * @param goodsQueryRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getToBeAddGoodsList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsResponse>> getToBeAddGoodsList(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getToBeAddGoodsList(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-查询货品列表(内部状态不为冻结)
     *
     * @param pageVO            分页实体
     * @param goodsQueryRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getDoesNotFreezeGoodsList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsResponse>> getDoesNotFreezeGoodsList(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getDoesNotFreezeGoodsList(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待新增物料-查询下级供应商货品列表(采购选品)
     *
     * @param pageVO                 分页实体
     * @param goodsQueryBySubRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getSubGoodsList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsSubResponse>> getSubGoodsList(PageVO pageVO, GoodsQueryBySubRequest goodsQueryBySubRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getSubGoodsList(sysUser, pageVO, goodsQueryBySubRequest);
        List<GoodsSubResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsSubResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待新增物料-查询货品信息
     *
     * @param id 货品id
     * @return Wrapper<?>
     */
    @RequestMapping(value = "/getGoods", method = RequestMethod.GET)
    public Wrapper<GoodsResponse> getGoods(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Goods goods = goodsService.getGoods(sysUser, id);
        if (goods != null) {
            //设置内部状态
            if (goods.getExternalState() != null && goods.getExternalState() > 0) {
                goods.setInteriorState(goods.getExternalState());
            }
            return Wrapper.success(this.modelMapper.map(goods, GoodsResponse.class));
        } else {
            return Wrapper.success(null);
        }
    }


    /**
     * 物料管理-查询货品信息(同时返回工作流信息)
     *
     * @param id 货品id
     * @return Wrapper<?>
     */
    @RequestMapping(value = "/getGoodsProcessDetail", method = RequestMethod.GET)
    public Wrapper<GoodsAndProcessResponse> getGoodsProcessDetail(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Goods goods = goodsService.getGoods(sysUser, id);
        if (goods != null) {
            GoodsAndProcessResponse goodsResponse = this.modelMapper.map(goods, GoodsAndProcessResponse.class);
            //处理数据
            goodsService.getProcessInfo(goods, sysUser, goodsResponse);
            return Wrapper.success(goodsResponse);
        } else {
            return Wrapper.success(null);
        }
    }


    /**
     * 物料管理-待新增物料-新增/修改货品
     *
     * @param goodsRequest 货品实体
     * @return 操作结果
     */
    @RequestMapping(value = "/saveOrUpdateGoods", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateGoods(@RequestBody @Valid GoodsRequest goodsRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        customerAttributeValueService.saveGoodsCustomerAttributeValue(sysUser, goodsRequest);
        return Wrapper.success(goodsService.saveOrUpdateGoods(sysUser, goodsRequest));
    }

    /**
     * 物料管理-待新增物料-批量删除货品
     *
     * @param simpleIdListRequest 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "deleteBatchGoods", method = RequestMethod.POST)
    public Wrapper<String> deleteBatchGoods(@RequestBody @Validated SimpleIdListRequest simpleIdListRequest) {
        return Wrapper.success(goodsService.deleteBatchGoods(getSysUser(), simpleIdListRequest.getIdList()));
    }

    /**
     * 物料管理-待新增物料-提交审核物料单
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/submit")
    public Wrapper<Void> goodsSubmit(@Validated @RequestBody SimpleIdRequest request) {
        return goodsService.goodsSubmit(request, getSysUser());
    }

    /**
     * 物料管理-待审核物料(一级)-列表
     *
     * @param pageVO            分页参数
     * @param goodsQueryRequest 请求参数
     * @return 操作结果
     */
    @GetMapping("/goodsExamineList1")
    public Wrapper<PageData<GoodsResponse>> goodsExamineList1(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.goodsExamineList1(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待审核物料(一级)-审核
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamine1")
    public Wrapper<Void> goodsExamine1(@Validated @RequestBody MaterialExamineRequest request) {
        return goodsService.goodsExamine1(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料(一级)-批量审核通过
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineBatch1")
    public Wrapper<Void> goodsExamineBatch1(@Validated @RequestBody SimpleIdListRequest request) {
        return goodsService.goodsExamineBatch1(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料(二级)-列表
     *
     * @param pageVO            分页参数
     * @param goodsQueryRequest 请求参数
     * @return 操作结果
     */
    @GetMapping("/goodsExamineList2")
    public Wrapper<PageData<GoodsResponse>> goodsExamineList2(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.goodsExamineList2(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待审核物料(二级)-审核
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamine2")
    public Wrapper<Void> goodsExamine2(@Validated @RequestBody MaterialExamineRequest request) {
        return goodsService.goodsExamine2(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料(二级)-批量审核通过
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineBatch2")
    public Wrapper<Void> goodsExamineBatch2(@Validated @RequestBody SimpleIdListRequest request) {
        return goodsService.goodsExamineBatch2(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料变更(一级)-列表
     *
     * @param pageVO            分页参数
     * @param goodsQueryRequest 请求参数
     * @return 操作结果
     */
    @GetMapping("/goodsExamineChangeList1")
    public Wrapper<PageData<GoodsResponse>> goodsExamineChangeList1(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.goodsExamineChangeList1(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待审核物料变更(一级)-审核
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineChange1")
    public Wrapper<Void> goodsExamineChange1(@Validated @RequestBody MaterialExamineRequest request) {
        return goodsService.goodsExamineChange1(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料变更(一级)-批量审核通过
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineChangeBatch1")
    public Wrapper<Void> goodsExamineChangeBatch1(@Validated @RequestBody SimpleIdListRequest request) {
        return goodsService.goodsExamineChangeBatch1(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料变更(二级)-列表
     *
     * @param pageVO            分页参数
     * @param goodsQueryRequest 请求参数
     * @return 操作结果
     */
    @GetMapping("/goodsExamineChangeList2")
    public Wrapper<PageData<GoodsResponse>> goodsExamineChangeList2(PageVO pageVO, GoodsQueryRequest goodsQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.goodsExamineChangeList2(sysUser, pageVO, goodsQueryRequest);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>() {
        }.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料管理-待审核物料变更(二级)-审核
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineChange2")
    public Wrapper<Void> goodsExamineChange2(@Validated @RequestBody MaterialExamineRequest request) {
        return goodsService.goodsExamineChange2(getSysUser(), request);
    }

    /**
     * 物料管理-待审核物料变更(二级)-批量审核通过
     *
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/goodsExamineChangeBatch2")
    public Wrapper<Void> goodsExamineChangeBatch2(@Validated @RequestBody SimpleIdListRequest request) {
        return goodsService.goodsExamineChangeBatch2(getSysUser(), request);
    }

    /************************************************请购单使用****************************************************/

    /**
     * 物料管理-查询货品列表(同时返回生产厂家和产地)
     *
     * @param pageVO                  分页实体
     * @param goodsSourceQueryRequest 货品查询类
     * @return 操作结果
     */
    @RequestMapping(value = "/getGoodsByMemberList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsSourceResponse>> getGoodsByMemberList(PageVO pageVO, GoodsSourceQueryRequest goodsSourceQueryRequest) {
        return Wrapper.success(goodsService.getGoodsByMemberList(pageVO, getSysUser(), goodsSourceQueryRequest));
    }

}
